#!/bin/bash

. ./path.sh || exit 1;
cd ..

average_num=10
decode_modes="salmonn_decode"
HOST_NODE_ADDR="localhost:0"
num_nodes=1
deepspeed_config=conf/ds_stage2.json
deepspeed_save_states="model_only"
export CUDA_VISIBLE_DEVICES="0,1,2,3,4,5,6,7"
stage=5 # start from 0 if you need to start from data_list preparation
stop_stage=6
num_workers=8  # 数据加载的进程数
prefetch=200
data_type=shard # raw or shard

dir=
train_config=
decode_checkpoint=
decode_checkpoint_name=
gpu_id=
test_data_dir=
test_sets=
key=
. tools/parse_options.sh || exit 1;
test_sets=$(echo "$test_sets" | sed 's/--/ /g')

echo "开始打印主要变量，这些变量有命令行传入"
echo "dir=$dir"
echo "train_config=$train_config"
echo "decode_checkpoint=$decode_checkpoint"
echo "decode_checkpoint_name=$decode_checkpoint_name"
echo "gpu_id=$gpu_id"
echo "stage=$stage"
echo "stop_stage=$stop_stage"
echo "mode=$mode"
echo "key=$key"
for test_set in $test_sets; do
{
    echo "prepare test this dataset: $test_set"
}
done
wait
set -e
set -u
set -o pipefail


if [ ${stage} -le 5 ] && [ ${stop_stage} -ge 5 ]; then
  decoding_chunk_size=
  ctc_weight=0.5
  echo "decoding ........."
  for test_set in $test_sets; do
  {
    echo "耿雪龙：test this dataset: $test_set"
    test_dir=$dir/test_${decode_checkpoint_name}/${test_set}
    mkdir -p $test_dir
    if [ -f "$test_dir/wer" ] && [ -s "$test_dir/wer" ] && [ $(wc -c < "$test_dir/text") -gt 20 ]; then
      echo "$test_set has been decoded! don't need to decode again!"
      continue
    fi
    export CUDA_VISIBLE_DEVICES=$gpu_id
    python wenet/bin/recognize_given_prompt.py --gpu $gpu_id \
      --modes $decode_modes \
      --config $train_config \
      --data_type "raw" \
      --test_data $test_data_dir/$test_set/data.list \
      --checkpoint $decode_checkpoint \
      --beam_size 10 \
      --batch_size 1 \
      --penalty 0.0 \
      --result_dir $test_dir \
      --ctc_weight $ctc_weight \
      --dataset $test_set \
      --key $key \
      --prompt_map_file './infer_one_prompt/prompt.yaml'

    echo "$test_set has been decoded!"

    if [ -f "$test_dir/text" ] && [ -s "$test_dir/text" ] && [ $(wc -c < "$test_dir/text") -gt 1 ]; then
        echo "$test_dir/text文件存在并且内容长度大于1"
        echo "compute wer this dataset: $test_set"
        python tools/compute-wer.py --char=1 --v=1 \
          $test_data_dir/$test_set/text $test_dir/text > $test_dir/wer
    else
        echo "$test_dir/text文件不存在或内容长度不大于1"
    fi
  }
  done
  wait
fi













if [ ${stage} -le 6 ] && [ ${stop_stage} -ge 6 ]; then
  decoding_chunk_size=
  ctc_weight=0.5
  for test_set in "${test_sets[@]}"; do
  {
    echo "compute wer this dataset: $test_set"
    test_dir=$dir/test_${decode_checkpoint_name}/${test_set}
    python tools/compute-wer.py --char=1 --v=1 \
      $test_data_dir/$test_set/text $test_dir/text > $test_dir/wer
    echo "$test_set has been decoded!"
  }
  done
  wait

fi
